import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import play.*;
import play.cache.Cache;
import play.jobs.*;
import play.test.*;
import models.*;

@OnApplicationStart
public class Bootstrap extends Job {
    public void doJob() {
        // Check if the database is empty
        if(User.count() == 0) {
           //Fixtures.load("data.yml");
        	User u1 = new User();
        	u1.email = "aicha.mangane@gmail.com";
        	u1.name="Aicha";
        	u1.password = "mangane";
        	u1.isAdmin = true;
        	u1.save();
        	
        	String mdp = "secret";
    		User jonathan = new User("Jonathan",mdp,"email@email.com");
    		jonathan.save();
    		User florent = new User();
    		florent.name = "Florent";
    		florent.password = mdp;
    		florent.email =  "email@email.com";
    		
    		florent.save();

    		List<User> doctorants = Arrays.asList(jonathan, florent);

    		Event coursPlay = new Event();
    		coursPlay.name = "cours de Play";
    		coursPlay.author = jonathan;
    		coursPlay.participants = doctorants;

    		Event coursJavascript = new Event();
    		coursJavascript.name = "cours de Javascript";
    		coursJavascript.author =  florent;
    		coursJavascript.participants = doctorants;
    		// Set the date of the previously created courses

    		Calendar cal = Calendar.getInstance(); // creates calendar
    		cal.setTime(new Date()); // sets calendar time/date
    		cal.getTime(); // returns new date object, one hour in the future

    		// Play! course begins now
    		coursPlay.begin = cal.getTime();
    		cal.add(Calendar.HOUR_OF_DAY, 2); // adds two hours
    		// Play! course ends in two hours
    		coursPlay.end = cal.getTime();
    		coursPlay.save();

    		cal.add(Calendar.HOUR_OF_DAY, 2);
    		// Javascript course begins two hours after Play! course
    		coursJavascript.begin = cal.getTime();
    		cal.add(Calendar.HOUR_OF_DAY, 2); // adds two hours
    		// Javascript course will length two hours
    		coursJavascript.end = cal.getTime();
    		coursJavascript.save();
        	
        	ArrayList<User> listeUsers = new ArrayList<User>();
        	listeUsers.add(u1);
        	Cache.set("Users", listeUsers);
        }
    }
}